package com.qs.javame.collection;

import java.util.*;

/**
 * 测试：Comparable接口
 */
public class TestComparable {

    public static void main(String[] args) {
        Set<Person> treeSet = new TreeSet<>();
        treeSet.add(new Person(20, "zhangsan"));
        treeSet.add(new Person(10, "lisi"));
        treeSet.add(new Person(21, "wangwu"));
        System.out.println("--->>排序后：" + treeSet);

        Person[] persons = new Person[3];
        persons[0] = new Person(20, "zhangsan");
        persons[1] = new Person(10, "lisi");
        persons[2] = new Person(21, "wangwu");
        Arrays.sort(persons);
        for (Person person : persons) {
            System.out.println(person);
        }
    }

    /**
     * 自定义类实现Comparable接口，实现compareTo()方法，稍后将该类的实例加入有序的集合中，集合中维护的顺序就是自定义的顺序；
     */
    private static class Person implements Comparable<Person> {

        private Integer age;

        private String name;

        public Person(Integer age, String name) {
            this.age = age;
            this.name = name;
        }

        public Person() {

        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public int compareTo(Person o) {
            return o.age.compareTo(this.age);
        }

        @Override
        public String toString() {
            return "Person{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}

/**
 * 测试：Comparator比较器接口
 */
class TestComparator {

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person(20, "张三"));
        personList.add(new Person(10, "李四"));
        personList.add(new Person(21, "王五"));
        System.out.println("排序前：" + personList);

        //对集合中的对象进行排序，可以自定义实现Comparator接口，实现比较器，实现compare(o1,o2)接口。
        personList.sort(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge().compareTo(o1.getAge());
            }
        });
        System.out.println("排序后：" + personList);
    }


    //自定义类中不实现Comparable接口
    private static class Person {

        private Integer age;

        private String name;

        public Person(Integer age, String name) {
            this.age = age;
            this.name = name;
        }

        public Person() {

        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
